<template>
  <div class="container">
    <div class="app-container" style="padding: 20px 20px;">
      <el-row type="flex" justify="start" align="middle">
        <span>楼宇管理：</span>
        <el-input v-model="selectName" size="mini" style="width: 200px; margin: 0 10px;" placeholder="请输入楼宇名称" />
        <el-button size="mini" type="primary" @click="BuildingList">查询</el-button>
      </el-row>
      <el-divider />
      <el-button style="margin-bottom: 10px;" size="mini" type="primary" @click="isShowDialog = true">添加楼宇</el-button>
      <el-table v-loading="loading" :data="list" style="width: 100%;">
        <el-table-column label="序号">
          <template #default="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="楼宇名称" />
        <el-table-column prop="floors" label="层数" />
        <el-table-column prop="area" label="在管面积(m²)" />
        <el-table-column prop="propertyFeePrice" label="物业费(元/m²)" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            {{ row.status ? '租赁中' : '空置中' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" size="medium" @click="edit(row.id)">编辑</el-button>
            <el-button type="text" size="medium" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" align="middle" style="margin-top: 20px;">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <el-dialog :title="showTitle" :visible.sync="isShowDialog" width="35%" @close="closeDialog">
      <el-form ref="addForm" :model="addForm" :rules="addRules" label-width="120px">
        <el-form-item label="楼宇名称" prop="name">
          <el-input v-model="addForm.name" size="small" style="width: 80%;" />
        </el-form-item>
        <el-form-item label="楼宇层数" prop="floors">
          <el-input v-model="addForm.floors" size="small" style="width: 80%;" />
        </el-form-item>
        <el-form-item label="在管面积" prop="area">
          <el-input v-model="addForm.area" size="small" style="width: 80%;">
            <template slot="append">m²</template>
          </el-input>
        </el-form-item>
        <el-form-item label="物业费" prop="propertyFeePrice">
          <el-input v-model="addForm.propertyFeePrice" size="small" style="width: 80%;">
            <template slot="append">元/m²</template>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="text" @click="isShowDialog = false">取消</el-button>
        <el-button size="mini" type="primary" style="margin-left: 25px;" @click="btnOk">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addBuilding, delBuilding, getBuildingDetail, getBuildingList, updateBuilding } from '@/api'

export default {
  name: 'ParkManagement',
  data() {
    return {
      loading: true,
      list: [],
      total: 0,
      currentPage: 1,
      page: 1,
      pageSize: 10,
      isShowDialog: false,
      selectName: '',
      addForm: {
        name: '',
        floors: '',
        area: '',
        propertyFeePrice: ''
      },
      addRules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入在管面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入物业费', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.addForm.id ? '编辑楼宇' : '添加楼宇'
    }
  },
  created() {
    this.BuildingList()
  },
  methods: {
    async BuildingList() {
      const { total, rows } = await getBuildingList({ name: this.selectName, page: this.page, pageSize: this.pageSize })
      this.list = rows
      this.total = total
      this.loading = false
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.BuildingList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.BuildingList()
    },
    async btnOk() {
      await this.$refs.addForm.validate()
      if (this.addForm.id) {
        await updateBuilding(this.addForm)
      } else {
        await addBuilding(this.addForm)
      }
      this.isShowDialog = false
      this.BuildingList()
    },
    closeDialog() {
      this.$refs.addForm.resetFields()
      this.addForm = {
        name: '',
        floors: '',
        area: '',
        propertyFeePrice: ''
      }
    },
    async del(id) {
      await this.$confirm('确认删除吗？')
      await delBuilding(id)
      if (this.list.length === 1) {
        this.page -= 1
      }
      this.BuildingList()
    },
    async edit(id) {
      this.addForm = await getBuildingDetail(id)
      this.isShowDialog = true
    }
  }
}
</script>
